package org.example.demo.dao;

import org.example.demo.entity.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DepartmentDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public int add(Department department){
        return jdbcTemplate.update("insert into department(bmname) values (?)",department.getBmname());

    }

    public int del(Integer id){
        return jdbcTemplate.update("delete from department where id = ?",id);
    }

    public int updatew(Department department){
        return jdbcTemplate.update("update department set bmname = ? where id = ?",department.getBmname(),department.getId());
    }

    public Department get(Integer id) {
        return jdbcTemplate.queryForObject("select * from department where id = ?", BeanPropertyRowMapper.newInstance(Department.class),id);
    }

    public List<Department> getAll(){
        return jdbcTemplate.query("select * from department ",BeanPropertyRowMapper.newInstance(Department.class));
    }
}
